﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Default3 : System.Web.UI.Page
{
    ManufacturerBus manuBU = new ManufacturerBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            this.loadManuFacture();

        }
    }

    public void loadManuFacture()
    {
        dlManuFac.DataSource = manuBU.getAllManufacturer();
        dlManuFac.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    protected void dlManuFac_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int idManu = Convert.ToInt32(dlManuFac.DataKeys[e.Item.ItemIndex]);
        string nameManu = ((TextBox)e.Item.FindControl("txtNameManu")).Text.ToString();
        string img = "~/images/" + ((Label)dlManuFac.Items[dlManuFac.EditItemIndex].FindControl("lblFileInfo")).Text.ToString();
        Manufacturer manu = new Manufacturer();
        manu.Name = nameManu;
        manu.Manufacturer_id = idManu;
        manu.Image = img;
        string mess = manuBU.InsertUpdateManufacturer(manu);
        dlManuFac.EditItemIndex = -1;
        this.loadManuFacture();
    }
    protected bool checkFile(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".jpg":
                return true;
            case ".bmp":
                return true;
            default:
                return false;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string file = "";
        FileUpload fileUp = (FileUpload)dlManuFac.Items[dlManuFac.EditItemIndex].FindControl("fileUpload");
        Image imgImage = (Image)dlManuFac.Items[dlManuFac.EditItemIndex].FindControl("Image");

        if (fileUp.HasFile)
        {
            if (checkFile(fileUp.FileName))
            {
                try
                {
                    file = fileUp.FileName.ToString().Trim();
                    //Response.Write(file);
                    if (File.Exists(Request.MapPath("~") + "\\images\\" + Server.UrlEncode(file)))
                    {
                        file = fileUp.FileName.ToString().Trim();
                        imgImage.Visible = true;
                        imgImage.ImageUrl = "~/images/" + Server.UrlEncode(file);

                        //Response.Write(file);
                    }
                    fileUp.SaveAs(Request.PhysicalApplicationPath + "images\\" + Server.UrlEncode(file));
                    imgImage.Visible = true;
                    imgImage.ImageUrl = "~/images/" + Server.UrlEncode(file);
                    ((Label)dlManuFac.Items[dlManuFac.EditItemIndex].FindControl("lblFileInfo")).Text = fileUp.FileName;

                }
                catch (System.Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        dlManuFac.EditItemIndex = -1;
        this.loadManuFacture();
    }
  

    protected void dlManuFac_DeleteCommand(object source, DataListCommandEventArgs e)
    {

        int ID = Convert.ToInt32(dlManuFac.DataKeys[e.Item.ItemIndex]);
        int result = manuBU.DeleteManuFactureBU(ID);
        this.loadManuFacture();
    }
    protected void dlManuFac_EditCommand(object source, DataListCommandEventArgs e)
    {
        int id = (int)e.Item.ItemIndex;
        dlManuFac.EditItemIndex = id;
        loadManuFacture();
    }
}